/*
 * Filename:    indicationRegionDeptHelp.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月20日
 */
package com.paic.mhis.hcpms.quota.settlement.biz.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiCateDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiDeptDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiDoctorDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiGradeDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiMedicalOrgDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.settlement.common.KpiValueConst;

/**
 * echarts 查询帮助类
 * 
 * @author DUQIFANG262
 * 
 */
@Component("kpiChartHelp")
public class KpiChartHelp {

	/**
	 * 根据开始日期以及结束日期 获取日期之间的集合
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<String> getTimeList(String startDate, String endDate) {
		List<String> timeList = new ArrayList<String>();
		String[] startDates = startDate.split("-");
		String[] endDates = endDate.split("-");
		int startYear = Integer.parseInt(startDates[0]);
		int startMonth = Integer.parseInt(startDates[1]);
		int endYear = Integer.parseInt(endDates[0]);
		int endMonth = Integer.parseInt(endDates[1]);
		int monthSize = (endYear - startYear) * 12 + endMonth - startMonth + 1;
		int year = startYear;
		int month = startMonth;

		for (int i = 0; i < monthSize; i++) {
			if (month > 12) {
				year = year + 1;
				month = 1;
			}
			if (month < 10) {
				timeList.add(year + "-0" + month);
			} else {
				timeList.add(year + "-" + month);
			}
			month++;
		}

		return timeList;
	}

	/**
	 * 转换为以 万 为单位
	 * 
	 * @return
	 */
	public BigDecimal transformInfo(BigDecimal value) {
		return value.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
	}

	private String intoAMillion(BigDecimal value, String kpiId) {
		boolean isFlag = false;
		// 判断是否需要转换为万为单位的指标集合
		for (String kpiCode : KpiValueConst.KPI_LIST) {
			if (kpiCode.equals(kpiId)) {
				isFlag = true;
				break;
			}
		}
		if (isFlag) {
			value = value.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);

		} else {
			for (String kpiCode : KpiValueConst.KPI_LIST2) {
				if (kpiCode.equals(kpiId)) {
					isFlag = true;
					break;
				}
			}
			if (!isFlag) {
				value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
			}
		}
		return value.toString();
	}

	/**
	 * 赋值 统筹区 显示值
	 * 
	 * @param kpiIdList
	 * @param kpiSociationList
	 * @return
	 */
	public Map<String, Object> processKpiSociationMent(List<String> kpiIdList, List<KpiSociationDTO> kpiSociationList,
			String startDate, String endDate) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (String kpiId : kpiIdList) {
			List<String> timeList = this.getTimeList(startDate, endDate);
			List<String> valueList = new ArrayList<String>();
			for (String kpiTime : timeList) {
				valueList.add(this.processKpiSociationValue(kpiSociationList, kpiTime, kpiId));
			}
			resultMap.put(kpiId, valueList);
		}
		return resultMap;
	}

	/**
	 * 统筹区 根据日期与指标 获取值
	 * 
	 * @param kpiSociationList
	 * @param kpiTime
	 * @param kpiId
	 */
	private String processKpiSociationValue(List<KpiSociationDTO> kpiSociationList, String kpiTime, String kpiId) {
		String valueStr = "0.00";
		try {
			BigDecimal kpiValue = new BigDecimal(0.00);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date kpiDate = sdf.parse(kpiTime);
			for (KpiSociationDTO kpiSociation : kpiSociationList) {
				Date kpiGradeDate = sdf.parse(kpiSociation.getKpiTime());
				if (kpiId.equals(kpiSociation.getKpiCode()) && kpiDate.equals(kpiGradeDate)) {
					kpiValue = kpiSociation.getKpiValue();
					break;
				}
			}
			valueStr = this.intoAMillion(kpiValue, kpiId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueStr;
	}

	/**
	 * 赋值 机构等级 显示值
	 * 
	 * @param kpiIdList
	 * @param kpiGradeList
	 * @return
	 */
	public Map<String, Object> processKpiGradeMent(List<String> kpiIdList, List<KpiGradeDTO> kpiGradeList,
			String startDate, String endDate) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (String kpiId : kpiIdList) {
			List<String> timeList = this.getTimeList(startDate, endDate);
			List<String> valueList = new ArrayList<String>();
			for (String kpiTime : timeList) {
				valueList.add(this.processKpiGradeValue(kpiGradeList, kpiTime, kpiId));
			}
			resultMap.put(kpiId, valueList);
		}
		return resultMap;
	}

	/**
	 * 机构等级 根据日期与指标 获取值
	 * 
	 * @param kpiGradeList
	 * @param kpiTime
	 * @param kpiId
	 */
	private String processKpiGradeValue(List<KpiGradeDTO> kpiGradeList, String kpiTime, String kpiId) {
		String valueStr = "0.00";
		try {
			BigDecimal kpiValue = new BigDecimal(0.00);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date kpiDate = sdf.parse(kpiTime);
			for (KpiGradeDTO kpiGrade : kpiGradeList) {
				Date kpiGradeDate = sdf.parse(kpiGrade.getKpiTime());
				if (kpiId.equals(kpiGrade.getKpiCode()) && kpiDate.equals(kpiGradeDate)) {
					kpiValue = kpiGrade.getKpiValue();
					break;
				}
			}
			valueStr = this.intoAMillion(kpiValue, kpiId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueStr;
	}

	/**
	 * 赋值 机构类别 显示值
	 * 
	 * @param kpiIdList
	 * @param kpiCateList
	 * @return
	 */
	public Map<String, Object> processKpiCateMent(List<String> kpiIdList, List<KpiCateDTO> kpiCateList,
			String startDate, String endDate) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (String kpiId : kpiIdList) {
			List<String> timeList = this.getTimeList(startDate, endDate);
			List<String> valueList = new ArrayList<String>();
			for (String kpiTime : timeList) {
				valueList.add(this.processKpiCateValue(kpiCateList, kpiTime, kpiId));
			}
			resultMap.put(kpiId, valueList);
		}
		return resultMap;
	}

	/**
	 * 机构类别 根据日期与指标 获取值
	 * 
	 * @param kpiCateList
	 * @param kpiTime
	 * @param kpiId
	 */
	private String processKpiCateValue(List<KpiCateDTO> kpiCateList, String kpiTime, String kpiId) {
		String valueStr = "0.00";
		try {
			BigDecimal kpiValue = new BigDecimal(0.00);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date kpiDate = sdf.parse(kpiTime);
			for (KpiCateDTO kpiCate : kpiCateList) {
				Date kpiGradeDate = sdf.parse(kpiCate.getKpiTime());
				if (kpiId.equals(kpiCate.getKpiCode()) && kpiDate.equals(kpiGradeDate)) {
					kpiValue = kpiCate.getKpiValue();
					break;
				}
			}
			valueStr = this.intoAMillion(kpiValue, kpiId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueStr;
	}

	/**
	 * 赋值 医疗机构 显示值
	 * 
	 * @param kpiIdList
	 * @param kpiMedicalOrgList
	 * @return
	 */
	public Map<String, Object> processKpiMedicalOrgMent(List<String> kpiIdList,
			List<KpiMedicalOrgDTO> kpiMedicalOrgList, String startDate, String endDate) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (String kpiId : kpiIdList) {
			List<String> timeList = this.getTimeList(startDate, endDate);
			List<String> valueList = new ArrayList<String>();
			for (String kpiTime : timeList) {
				valueList.add(this.processKpiMedicalOrgValue(kpiMedicalOrgList, kpiTime, kpiId));
			}
			resultMap.put(kpiId, valueList);
		}
		return resultMap;
	}

	/**
	 * 医疗机构 根据日期与指标 获取值
	 * 
	 * @param kpiMedicalOrgList
	 * @param kpiTime
	 * @param kpiId
	 */
	private String processKpiMedicalOrgValue(List<KpiMedicalOrgDTO> kpiMedicalOrgList, String kpiTime, String kpiId) {
		String valueStr = "0.00";
		try {
			BigDecimal kpiValue = new BigDecimal(0.00);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date kpiDate = sdf.parse(kpiTime);
			for (KpiMedicalOrgDTO kpiMedicalOrg : kpiMedicalOrgList) {
				Date kpiGradeDate = sdf.parse(kpiMedicalOrg.getKpiTime());
				if (kpiId.equals(kpiMedicalOrg.getKpiCode()) && kpiDate.equals(kpiGradeDate)) {
					kpiValue = kpiMedicalOrg.getKpiValue();
					break;
				}
			}
			valueStr = this.intoAMillion(kpiValue, kpiId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueStr;
	}

	/**
	 * 赋值 科室维度 显示值
	 * 
	 * @param kpiIdList
	 * @param kpiDeptList
	 * @return
	 */
	public Map<String, Object> processKpiDeptOrgMent(List<String> kpiIdList, List<KpiDeptDTO> kpiDeptList,
			String startDate, String endDate) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (String kpiId : kpiIdList) {
			List<String> timeList = this.getTimeList(startDate, endDate);
			List<String> valueList = new ArrayList<String>();
			for (String kpiTime : timeList) {
				valueList.add(this.processKpiKpiDeptValue(kpiDeptList, kpiTime, kpiId));
			}
			resultMap.put(kpiId, valueList);
		}
		return resultMap;
	}

	/**
	 * 科室维度 根据日期与指标 获取值
	 * 
	 * @param kpiDeptList
	 * @param kpiTime
	 * @param kpiId
	 */
	private String processKpiKpiDeptValue(List<KpiDeptDTO> kpiDeptList, String kpiTime, String kpiId) {
		String valueStr = "0.00";
		try {
			BigDecimal kpiValue = new BigDecimal(0.00);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date kpiDate = sdf.parse(kpiTime);
			for (KpiDeptDTO kpiDept : kpiDeptList) {
				Date kpiGradeDate = sdf.parse(kpiDept.getKpiTime());
				if (kpiId.equals(kpiDept.getKpiCode()) && kpiDate.equals(kpiGradeDate)) {
					kpiValue = kpiDept.getKpiValue();
					break;
				}
			}
			valueStr = this.intoAMillion(kpiValue, kpiId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueStr;
	}

	/**
	 * 赋值 医生维度 显示值
	 * 
	 * @param kpiIdList
	 * @param kpiDeptList
	 * @return
	 */
	public Map<String, Object> processKpiDoctorOrgMent(List<String> kpiIdList, List<KpiDoctorDTO> kpiDoctorList,
			String startDate, String endDate) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		for (String kpiId : kpiIdList) {
			List<String> timeList = this.getTimeList(startDate, endDate);
			List<String> valueList = new ArrayList<String>();
			for (String kpiTime : timeList) {
				valueList.add(this.processKpiKpiDoctorValue(kpiDoctorList, kpiTime, kpiId));
			}
			resultMap.put(kpiId, valueList);
		}
		return resultMap;
	}

	/**
	 * 医生维度 根据日期与指标 获取值
	 * 
	 * @param kpiDeptList
	 * @param kpiTime
	 * @param kpiId
	 */
	private String processKpiKpiDoctorValue(List<KpiDoctorDTO> kpiDoctorList, String kpiTime, String kpiId) {
		String valueStr = "0.00";
		try {
			BigDecimal kpiValue = new BigDecimal(0.00);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			Date kpiDate = sdf.parse(kpiTime);
			for (KpiDoctorDTO kpiDoctor : kpiDoctorList) {
				Date kpiGradeDate = sdf.parse(kpiDoctor.getKpiTime());
				if (kpiId.equals(kpiDoctor.getKpiCode()) && kpiDate.equals(kpiGradeDate)) {
					kpiValue = kpiDoctor.getKpiValue();
					break;
				}
			}
			valueStr = this.intoAMillion(kpiValue, kpiId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valueStr;
	}

	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("KPI0038");
		list.add("KPI0039");
		System.out.println(list.contains("KPI0038"));
	}

}
